define(function(require, exports, module) {
	'use strict';
    
    var rk = require('rk');

    require('../../su/accordion/oaTabCtrl');
    require('../../su/accordion/oaListCtrl');
    require('oa/js/workreport/asset/list/wrExportPopupCtrl');

    require('../search/wrSearchCurtainCtrl');

    $.widget('rk.wrSubPanelCtrl',{
            options:{
                
            },
            viewType: null, 
            _create: function(){
                var me = this;
                var elem = me.element;

                elem.data('wrSubPanelCtrl', 'true');
            },
            _init: function(){
                var me = this;
                var elem = me.element;

                me._initTitleBar();
                me._initTab();
            },
            _initTitleBar: function(){
                //手风琴面板
                var me = this;
                var elem = me.element;

                var titlebar = me.getTitleBar();
                var act = titlebar.attr('act');

                if(titlebar.data('inited') == 'true')return;
                titlebar.data('inited', 'true');

                me.viewType = act;

                var navMore = titlebar.find('.nav-more').parent();
                var navMenu = titlebar.find('.drop_div'); 
                navMore.rkEasyMenu({
                    menu: navMenu
                })

                titlebar.on('click', '*[act]', function(){
                    var btn = $(this);
                    if(act == 'mine' || act == 'received' || act == 'subordinate'){
                        if(btn.attr('act') == 'search'){ me._onSearchClick(act);  }
                        if(btn.attr('act') == 'export'){ me._onExportClick(act);  }
                    } 
                    if(act == 'received'){
                        if(btn.attr('act') == 'read_all'){ me._onMarkAllReadClick(act); }
                    }
                });
            },
            _initTab: function(){
                var me = this;
                var elem = me.element;
                var opt = me.options;
                var businessTypes = opt.businessTypes;
                var initResults = opt.results;

                var titlebar = me.getTitleBar();
                var act = titlebar.attr('act');
                
                //三个tabs按钮的配置
                var tabs = [];
                for(var i = 0; i < businessTypes.length; i++){
                    var biztype = businessTypes[i];
                    tabs.push({
                        title: biztype.typeName,//按钮标题
                        html_attrs: {           //按钮的html属性
                            belongTypeId: biztype.id,
                            belongId: biztype.belongId
                        },
                        //（可选），自动加载的选项
                        controller: 'wrListCtrl',//子页面应初始化成什么controller
                        controllerOptions: {
                            id: act + '_' + biztype.id,
                            viewtype: act
                        },
                        autoload: {
                            url: opt.dataUrl,
                            param: {
                                belongTypeId: biztype.id,
                                belongId: biztype.belongId
                            },
                            rowTemplateId: opt.templateId
                        }
                    });
                }

                //初始化tab
                elem.oaTabCtrl({
                    tabs: tabs,
                    onTabSelect: function(e, info){
                        rk.oa.closeContent();
                    },
                    onInit: function(){
                        //获取第一个list，绑定初始数据
                        elem.oaTabCtrl('select', 0);
                    }
                });
            },
            getTitleBar: function(){
                var me = this;
                var elem = me.element;

                return elem.prev();
            },
            setUnreadNum: function(num){
                var me = this;
                var numobj = me.getTitleBar().find('.unread-num');
                numobj.html(num);
            },
            _onSearchClick: function(){
                var me = this;
                var elem = me.element;

                rk.oa.popupCurtain(function(curtainEl){
                    var tpl_data ={};
                    tpl_data.businessTypes = workreportService.getBelongTypes()
                    tpl_data.business_group = me.viewType;
                    //debugger;
                    var options = {
                        templateId: 'oa/tmpl/workreport/tpl_workreport_search.tpl',
                        parentHolder: curtainEl,
                        data: tpl_data,
                        act:me.viewType
                    }
                    curtainEl.wrSearchCurtainCtrl(options);
                });
            },
            _onExportClick: function(){
                var me = this;
                var elem = me.element;

                rk.dialog({
                    title: rk.i18n('EXPORT'),
                    success: function (contentElem){
                        contentElem.wrExportPopupCtrl({
                            viewType: me.viewType,
                            belongTypeId: elem.oaTabCtrl('getSelectedTab').attr('belongTypeId')
                        });//初始化弹框内容的Ctrl
                    },
                    footer: ['cancel', {act: 'export', visible: true, text: rk.i18n('EXPORT')}]
                });
            },
            _onMarkAllReadClick: function(){
                var me = this;
                var elem = me.element;
                workreportService.sendApproval( 'all', {
                    belongTypeId: elem.oaTabCtrl('getSelectedTab').attr('belongTypeId')
                }, function(){
                    var list = elem.oaTabCtrl('getSelectedChild');
                    list.wrListCtrl('setApproved', 'all');
                });
            },
            _destroy: function(){
                var me = this;
                var elem = me.element;
                elem.oaTabCtrl('destroy').off().html('');
            }
    });
});